#!/usr/bin/python

#
# == Synopsis
#
# Creates a list of all the packages in google-code's bollin project
# which have been updated and need to be uploaded to Debian.
#
# == Usage
#
# updated_list.py > Updates.wiki
#
# == Author
#
# Varun Hiremath <varunhiremath@gmail.com>
#
# == Copyright
#
# Copyright (c) 2007 Varun Hiremath
# Licensed under the terms of the General Public license (GPL).
#

from BeautifulSoup import *
import urllib2, os
import threading, time

PQA= "http://packages.qa.debian.org/"

Header="""#summary A list of updated packages in bollin.
#labels Featured

Do not edit this file because it is automatically created by the script
[http://bollin.googlecode.com/svn/Bollin/updated_list.py updated_list.py] and
will be overwritten!

= List of Packages =

"""

print Header

def output(cmd):
    pipe = os.popen('{ ' + cmd + '; } 2> /dev/null', 'r')
    return pipe

class PackageInfo( threading.Thread ):
    def __init__(self, package):
        self.package = package
        threading.Thread.__init__ ( self )
    
    def run(self):
        try:
            Changelog = output("svn cat https://bollin.googlecode.com/svn/%s/trunk/debian/changelog | dpkg-parsechangelog -l- " % self.package).readlines()
            SOURCE = Changelog[0].split(":")[-1].strip()
        except:
            return
        
        if self.package[:3] == "lib":
            URL = PQA+SOURCE[:4]+"/"+SOURCE+".html"
        else:
            URL = PQA+SOURCE[0]+"/"+SOURCE+".html"

        try:
            u = urllib2.urlopen(URL)
            s = u.read()
            soup = BeautifulSoup(s)
            Debian_Version = soup.findAll('td',{'class':'contentcell'})[0].string.strip()
        except:
            Debian_Version = "None"

        try:
            Bollin_Version = Changelog[1].split(" ")[-1].strip()
            if cmp(Bollin_Version, Debian_Version) or Debian_Version=="None":
                Changes = Changelog[5:]
                print " # [https://bollin.googlecode.com/svn/%s/trunk %s] " % (self.package,self.package)
                print "   * Bollin Version: %s" % Bollin_Version
                print "   * Debian Version: %s" % Debian_Version
                LOG = []
                for line in Changes:
                    if line.strip()[0:4] in ["Date", "Chan", "Clos"]:
                        print "   * %s" % line.strip()
                    else:
                        LOG.append(line)

                print "     {{{"
                for line in LOG:
                        print "     %s" % line.strip()
                print "     }}}"
        except:
            return
    
packages = output("svn list https://bollin.googlecode.com/svn/").readlines()

for package in packages:
    package = package.strip()[:-1]
    PackageInfo(package).start()
    time.sleep(0.2)
